package com.local.admin.controller;

import com.local.admin.constants.AdminConstants;
import com.local.admin.pojo.AdminUser;
import com.local.admin.service.AdminUserService;
import com.local.common.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Objects;

@Controller
@RequestMapping("/admin/profile")
public class AdminProfileController {

    @Autowired
    private AdminUserService adminUserService;


    /**
     * 修改密码页面
     * @param request
     * @return
     */
    @GetMapping
    public String index(HttpServletRequest request) {
        Integer adminUserId = (Integer) request.getSession().getAttribute(AdminConstants.ADMIN_USER_ID_SESSION_KEY);
        AdminUser adminUser = adminUserService.selectById(adminUserId);
        if (Objects.isNull(adminUser)) {
            return "redirect:/admin/login";
        }

        request.setAttribute("path", "profile");
        request.setAttribute("loginUserName", adminUser.getLoginUserName());
        request.setAttribute("nickName", adminUser.getNickName());

        return "admin/profile";
    }


    /**
     * 修改用户名和昵称
     * @param loginUserName
     * @param nickName
     * @param session
     * @return
     */
    @PostMapping("/name")
    @ResponseBody
    public ResultUtil updateName(@RequestParam("loginUserName") String loginUserName,
                                 @RequestParam("nickName") String nickName, HttpSession session) {
        Integer loginUserId = Integer.valueOf(session.getAttribute(AdminConstants.ADMIN_USER_ID_SESSION_KEY).toString());
        Integer updateResult = adminUserService.updateName(loginUserId, loginUserName, nickName);
        if (updateResult == -1) {
            return new ResultUtil(201, "登录名："+loginUserName+" 已被占用，请换其他的登录名");
        } else if (updateResult > 0) {
            return new ResultUtil(200, "修改成功");
        } else {
            return new ResultUtil(201, "修改失败");
        }
    }


    /**
     * 修改密码
     * @param originalPassword
     * @param newPassword
     * @param session
     * @return
     */
    @PostMapping("/password")
    @ResponseBody
    public ResultUtil updatePassword(@RequestParam("originalPassword") String originalPassword,
                                     @RequestParam("newPassword") String newPassword,
                                     HttpSession session) {
        Integer adminUserId = Integer.valueOf(session.getAttribute(AdminConstants.ADMIN_USER_ID_SESSION_KEY).toString());
        Integer updateResult = adminUserService.updatePassword(adminUserId, originalPassword, newPassword);
        if (updateResult == -1) {
            return new ResultUtil(201, "参数错误");
        } else if (updateResult == -2){
            return new ResultUtil(201, "原密码错误");
        } else if (updateResult <= 0) {
            return new ResultUtil(201, "修改错误，请稍后再试");
        } else {
            return new ResultUtil(200, "修改成功");
        }
    }
}
